package com.psl.services.frame;

import com.psl.Dao.Conn;
import com.psl.model.userlogin;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class accessuser
{
  public List queryuser()
  {
    Connection conn = new Conn().getConn();
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    List user = new ArrayList();
    try
    {
      pstmt = conn.prepareStatement("SELECT u.userID,u.username,u.password,u.position_id,p.position FROM userlogin u,positionlist p where u.position_id=p.position_id");
      rs = pstmt.executeQuery();
      while (rs.next())
      {
        userlogin pt = new userlogin();
        pt.setUserID(rs.getString(1));
        pt.setUsername(rs.getString(2));
        pt.setPassword(rs.getString(3));
        pt.setPosition_id(rs.getInt(4));
        pt.setPosition(rs.getString(5));
        user.add(pt);
        HttpServletRequest request= ServletActionContext.getRequest();
        request.setAttribute("user2",user);
      }
      return user;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      return null;
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (rs != null) {
          rs.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }

  }
  
  public boolean deluser(String userID)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("DELETE from userlogin  WHERE userID=?");
      pstmt.setString(1, userID);
      pstmt.executeUpdate();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }
}
